#include "fdfs_client.h"
#include "fastcommon/logger.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "make_log.h"

int fdfs_upload_file(const char *confpath, const char *filename, char *fileid)
{
  char group_name[FDFS_GROUP_NAME_MAX_LEN + 1];
	ConnectionInfo *pTrackerServer;
	int result;
	int store_path_index;
	ConnectionInfo storageServer;

    // 加载配置文件, 并初始化
    const char* conf_file = confpath;
	if ((result=fdfs_client_init(conf_file)) != 0)
	{
		return result;
	}

    // 通过配置文件信息连接tracker, 并得到一个可以访问tracker的句柄
	pTrackerServer = tracker_get_connection();
	if (pTrackerServer == NULL)
	{
		fdfs_client_destroy();
		return errno != 0 ? errno : ECONNREFUSED;
	}

	*group_name = '\0';

    // 通过tracker句柄得到一个可以访问的storage句柄
	if ((result=tracker_query_storage_store(pTrackerServer, \
	                &storageServer, group_name, &store_path_index)) != 0)
	{
		fdfs_client_destroy();
		LOG("tracker_query_storage fail, error no: %d, error info: %s",
			result, STRERROR(result));
		return result;
	}


    // 通过得到的storage句柄, 上传本地文件
	result = storage_upload_by_filename1(pTrackerServer, \
			&storageServer, store_path_index, \
			filename, NULL, \
			NULL, 0, group_name, fileid);
	if (result == 0)
	{
        LOG("fileID = %s", fileid);
	}
	else
	{
        LOG("upload file fail, error no: %d, error info: %s",
			result, STRERROR(result));
	}

    // 断开连接, 释放资源
	tracker_close_connection_ex(pTrackerServer, true);
	fdfs_client_destroy();

	return result;
}
